<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/16
 * Time: 13:55
 */

namespace app\api\model;
use think\Controller;
use think\Model;
use think\Db;

class Message extends Model
{
    /*
     * 查询是否有消息 首页显示红点
     */
    public function is_read($uid)
    {
        $message =  Db::name('message')
            ->where('receive_user_id',$uid)
            ->where('is_read','0')
            ->select();
        $comment1 = Db::name("comment_message")->where("uid",$uid)->where("is_read",0)->select();
        $comment2 = Db::name("comment_method_message")->where('uid',$uid)->where('is_read',0)->select();
        $like = Db::name('log_like')->where('send_user',$uid)->where('is_read',0)->select();
        if($message || $comment1 || $comment2 || $like){
            return 1;
        }else{
            return 0;
        }
    }

    public function notice($uid,$type,$page)
    {
        if($type==1){
            //先将信息全部修改成已读
            Db::name('message')->where('receive_user_id',$uid)->where('msg_type',1)->setField('is_read',1);
            $res = Db::name('message')->field('msg_title,msg_content,oid,create_time')
                ->where('receive_user_id',$uid)->where('msg_type',1)
                ->limit(($page-1)*config('page_size'),config('page_size'))
                ->order('create_time','desc')
                ->select();
            foreach($res as $k=>$v){
                $res[$k]['create_time'] = date('Y-m-d',strtotime($v['create_time']));
            }
            return $res;

        }
        if($type==2){
            //先将信息全部修改成已读
            Db::name('message')->where('receive_user_id',$uid)->where('msg_type',2)->setField('is_read',1);
            $res = Db::name('message')->field('oid,msg_title,msg_content,create_time,type')
                ->where('receive_user_id',$uid)->where('msg_type',2)
                ->limit(($page-1)*config('page_size'),config('page_size'))
                ->order('create_time','desc')
                ->select();
            foreach($res as $k=>$v){
                $res[$k]['create_time'] = date('Y-m-d',strtotime($v['create_time']));
            }
            return $res;
        }
    }
}